import { data } from "./data1.mjs";

// 图的实现
class Graph {
  constructor() {
    this.vertices = new Set();
    this.edges = {};
  }

  // 添加节点
  addVertex(vertex) {
    if (this.vertices.has(vertex)) {
      return;
    }
    this.vertices.add(vertex);
    this.edges[vertex] = [];
  }

  // 添加边
  addEdge(vertex1, vertex2) {
    this.edges[vertex1].push(vertex2);
    // 如果是无向图，还需要添加反向的边
    // this.edges[vertex2].push(vertex1);
  }

  // 打印图的结构
  printGraph() {
    for (const vertex of this.vertices) {
      const edges = this.edges[vertex].join(', ');
      console.log(`${vertex} -> ${edges}`);
    }
  }
  // 深度优先搜索
  dfs(startVertex, visited = {}) {
    if (!this.vertices.has(startVertex)) {
      console.log('Vertex not found.');
      return;
    }

    visited[startVertex] = true;
    console.log(startVertex);

    for (const neighbor of this.edges[startVertex]) {
      if (!visited[neighbor]) {
        this.dfs(neighbor, visited);
      }
    }
  }

  // 广度优先搜索
  bfs(startVertex) {
    if (!this.vertices.has(startVertex)) {
      console.log('Vertex not found.');
      return;
    }

    const visited = {};
    const queue = [];

    visited[startVertex] = true;
    queue.push(startVertex);

    while (queue.length > 0) {
      const currentVertex = queue.shift();
      console.log(currentVertex);

      for (const neighbor of this.edges[currentVertex]) {
        if (!visited[neighbor]) {
          visited[neighbor] = true;
          queue.push(neighbor);
        }
      }
    }
  }

  // 深度优先搜索
  dfsPrint(startVertex, visited = {}) {
    if (!this.vertices.has(startVertex)) {
      console.log('Vertex not found.');
      return;
    }

    visited[startVertex] = true;
    console.log(startVertex);

    for (const neighbor of this.edges[startVertex]) {
      if (!visited[neighbor]) {
        this.dfsPrint(neighbor, visited);
      }
    }
  }

  // 打印图的结构，使用深度优先搜索
  printGraphDFS() {
    const visited = {};
    this.vertices.forEach((vertex) => {
      if (!visited[vertex]) {
        this.dfsPrint(vertex, visited);
      }
    })
  }
}

// 下游
const billGraph = new Graph();
billGraph.addVertex(data.curObject.id)
data.srcRelationships.forEach((item) => {
  billGraph.addVertex(item.fromObjectId);
  billGraph.addVertex(item.toObjectId);
  billGraph.addEdge(item.fromObjectId, item.toObjectId);
})
data.targetRelationships.forEach((item) => {
  billGraph.addVertex(item.fromObjectId);
  billGraph.addVertex(item.toObjectId);
  billGraph.addEdge(item.fromObjectId, item.toObjectId);
})

// 上游
const reversedBillGraph = new Graph()
reversedBillGraph.addVertex(data.curObject.id)
data.srcRelationships.forEach((item) => {
  reversedBillGraph.addVertex(item.fromObjectId);
  reversedBillGraph.addVertex(item.toObjectId);
  reversedBillGraph.addEdge(item.toObjectId, item.fromObjectId);
})
data.targetRelationships.forEach((item) => {
  reversedBillGraph.addVertex(item.fromObjectId);
  reversedBillGraph.addVertex(item.toObjectId);
  reversedBillGraph.addEdge(item.toObjectId, item.fromObjectId);
})

// 单据类型
const billTypeGroup = groupBy(data.objects, 'objectType')
const billTypeGraph = new Graph();
const mainBill = data.curObject.id;
billTypeGraph.addVertex(mainBill);
Object.entries(billTypeGroup).forEach(([objectType, bills]) => {
  billTypeGraph.addVertex(objectType) // 单据类型
  billTypeGraph.addEdge(mainBill, objectType) // 主单据到各单据类型的边
  bills.forEach((item) => {
    billTypeGraph.addVertex(item.id) // 单据类型包含的子单据
    billTypeGraph.addEdge(objectType, item.id) // 单据类型到子单据的边
  })
})

export { billTypeGraph, billGraph, reversedBillGraph, groupBy };

function groupBy(arr) {
  return arr.reduce((obj, item) => {
    obj[item.objectType] = obj[item.objectType] || []
    obj[item.objectType].push(item)
    return obj
  }, {})
}